package com.etc.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.etc.utils.Address;
import com.etc.utils.MapUtil;

/**
 * 首页操作的控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="Userapi")
public class IndexController {
	@RequestMapping(value="getAddress",method=RequestMethod.GET)
	@ResponseBody
	public void getAddressById(HttpSession session) {
		Address address= new Address();
		String url = "http://api.map.baidu.com/location/ip?&ak=2000&output=json&ak=IkB2sI3mEfxhf9ZjA7UDZFpw7QoHheTp";
		String result = MapUtil.connectURL(url, "");
		//JSON.parse(result);
		//System.out.println(result);
		JSONObject jo = JSONObject.parseObject(result);
		JSONObject joc = JSONObject.parseObject(jo.getString("content"));
		String addr= joc.getString("address");
		int n = addr.indexOf("省");
		String province = addr.substring(0, n+1);
		String  city = addr.substring(n+1, addr.length());
		address.setProvince(province);
		address.setCity(city);
		address.setAddress(addr);
		session.setAttribute("Address", address);
	}
	
}
